const API = require('../../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        gardenId:'',
        parkCompanyList:[],
        pageNum: 1,
        reachBottomShow: true,
        screenList_company: [
            {
                title: '城市',
                children: ['全部','杭州'],
                current: 0,
            },
            {
                title: '地区',
                children: ['全部','上城区','下城区','西湖区','江干区','拱墅区','滨江区','萧山区','余杭区','临安区','富阳区','建德','桐庐县','淳安县'],
                current: 0,
            },
            {
                title: '行业',
                children: ['全部','农、林、牧、渔业','采矿业', '制造业', '电力、燃气及水的生产和供应业', '建筑业', '交通运输、仓储和邮政业', '信息传输、计算机服务和软件业', '批发和零售业', '住宿和餐饮业', '金融业', '房地产业', '租赁和商务服务业', '科学研究、技术服务和地质勘查业', '水利、环境和公共设施管理业', '居民服务和其他服务业'],
                current: 0,
            },
            {
                title: '认证状态',
                children: ['全部','已认证','待认证','未认证'],
                current: 0,
            },
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            gardenId: options.id
        })
        this.get_park_company_list()
    },

    /**
     * 查询当前园区企业列表
     */
    get_park_company_list:function(){
        let that = this
        wx.showLoading({
          title: '加载中',
        })
        API.get_park_company_list({
            id: that.data.gardenId,
            pageNum: that.data.pageNum,
            pageSize: 10
        }).then( res => {
            if( res.code == 200 ){
                let parkCompanyList = res.rows.map( ele => {
                    ele.phone = ele.phone.substring(0,3) + '****' + ele.phone.substring(7,11)
                    if(ele.establishTime!=null){
                        ele.establishTime = ele.establishTime.split(' ')[0].replace(/-/g,'/')
                    }
                    if(ele.annalsSite!=null){
                        let siteIndex =  ele.annalsSite.indexOf('市') + 1
                        if(siteIndex != -1){
                            ele.annalsSite =  ele.annalsSite.substr(siteIndex)
                        }
                    }
                    return ele
                })
                that.setData({
                    parkCompanyList: [...that.data.parkCompanyList,...parkCompanyList]
                })
                // if( res.total <= that.data.pageNum * 10 ){
                //     that.setData({
                //         reachBottomShow: false
                //     })
                // }
                if( res.rows.length == 10 ){
                    that.setData({
                        reachBottomShow: true
                    })
                }else{
                    that.setData({
                        reachBottomShow: false
                    })
                }
                setTimeout(() => {
                    wx.hideLoading()
                }, 1000);
            } else {
                wx.hideLoading()
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }       
        })
    },

    /**
     * 事件 - 企业筛选改变
     */
    companyScreenChange:function(e){
        let index = e.currentTarget.id
        let children = e.detail.value
        this.setData({
            ['screenList_company[' + index + '].current']: children
        })
    },
    

    /**
     * 路由 - 企业
     */
    routeCompany:function(e){
        //登陆拦截
        if( !app.globalData.hasUserInfo ){
            wx.showToast({
                title: '您还没有登陆哦',
                icon:'none'
            })
              
            setTimeout( () => {
                wx.reLaunch({
                    url: '/pages/login/login?back=/pages/home/home',
                })
            },1000)
            return
        }
        //路由跳转
        let item = e.currentTarget.dataset.item
        if( item.sign != 2 || !item.enterpriseId ){
            wx.navigateTo({
                url: '/pages/park/company/company?id=' + item.id + '&type=2',
            })
            // wx.showToast({
            //   title: '该企业未认证，暂不可查看',
            //   icon:'none'
            // })
            return
        }
        console.log(item)
        wx.navigateTo({
            url: '/pages/park/company/company?id=' + item.enterpriseId,
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let that = this
        let pageNum = this.data.pageNum
        if( this.data.reachBottomShow ){
            pageNum++
            this.setData({
                pageNum: pageNum
            })
            that.get_park_company_list()
            console.log('加载下一页', pageNum)
        }else{
            console.log('到底了',pageNum, pageNum)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})